package org.dromara.xxmj.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.xxmj.domain.dto.ExerciseSubmitDto;
import org.dromara.xxmj.domain.dto.StudyProgressDto;
import org.dromara.xxmj.domain.vo.CourseExerciseVo;
import org.dromara.xxmj.domain.vo.CourseVideoDetailVo;
import org.dromara.xxmj.domain.vo.CourseVideoVo;
import org.dromara.xxmj.service.ICourseExerciseService;
import org.dromara.xxmj.service.ICourseVideoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程视频及习题控制器
 *
 * @author Vincent
 */
@Slf4j
@Tag(name = "课程视频管理API")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xxmj/video")
public class CourseVideoController extends BaseController {

    private final ICourseVideoService videoService;
    private final ICourseExerciseService exerciseService;

    /**
     * 获取章节下的视频列表
     */
    @Operation(summary = "获取章节下的视频列表")
    @GetMapping("/list/{chapterId}")
    public R<List<CourseVideoVo>> listVideosByChapter(@Parameter(description = "章节ID") @PathVariable Long chapterId) {
        return R.ok(videoService.listVideosByChapter(chapterId));
    }

    /**
     * 获取视频详情
     */
    @Operation(summary = "获取视频详情")
    @GetMapping("/detail/{id}")
    public R<CourseVideoDetailVo> getVideoDetail(@Parameter(description = "视频ID") @PathVariable Long id) {
        return R.ok(videoService.getVideoDetail(id));
    }

    /**
     * 更新视频观看进度
     */
    @Operation(summary = "更新视频观看进度")
    @PostMapping("/progress")
    public R<Boolean> updateProgress(@RequestBody @Valid StudyProgressDto dto) {
        return R.ok(videoService.updateProgress(dto));
    }

    /**
     * 获取视频关联的习题列表
     */
    @Operation(summary = "获取视频关联的习题列表")
    @GetMapping("/exercise/list/{videoId}")
    public R<List<CourseExerciseVo>> listExercisesByVideo(@Parameter(description = "视频ID") @PathVariable Long videoId,@Parameter(description = "课程ID") @RequestParam(required = false) Long courseId) {
        return R.ok(exerciseService.listExercisesByVideo(courseId,videoId));
    }

    /**
     * 获取习题详情
     */
    @Operation(summary = "获取习题详情")
    @GetMapping("/exercise/detail/{id}")
    public R<CourseExerciseVo> getExerciseDetail(@Parameter(description = "习题ID") @PathVariable Long id) {
        return R.ok(exerciseService.getExerciseDetail(id));
    }

    /**
     * 提交习题答案
     */
    @Operation(summary = "提交习题答案")
    @PostMapping("/exercise/submit")
    public R<Boolean> submitAnswer(@RequestBody @Valid ExerciseSubmitDto dto) {
        return R.ok(exerciseService.submitAnswer(dto));
    }

    /**
     * 获取习题完成度
     */
    @Operation(summary = "获取习题完成度")
    @GetMapping("/exercise/completedRate/{videoId}")
    public R<Integer> getUserExerciseCompletedRate(@Parameter(description = "视频ID") @PathVariable Long videoId) {
        return R.ok(exerciseService.getUserExerciseCompletedRate(videoId));
    }


}
